package com.cloudlead.common.worker.desc;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import com.cloudlead.common.worker.desc.datatype.FormData;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 模块定义
 *
 * @author dean.lu
 */
@XmlRootElement
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@JsonInclude(Include.NON_NULL)
public class ModuleDesc implements Comparable<ModuleDesc> {
    /**
     * 模块名称
     */
    private String name;
    /**
     * 注释
     */
    private String comment;

    /**
     * 排序
     */
    private int ordinal;
    /**
     * 所属模块
     */
    private ModuleDesc parent;
    /**
     * 该模块相关的数据
     */
    private FormData data;
    /**
     * 模型相关的接口
     */
    private Set<String> moduleFacades;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getOrdinal() {
        return ordinal;
    }

    public void setOrdinal(int ordinal) {
        this.ordinal = ordinal;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public ModuleDesc getParent() {
        return parent;
    }

    public void setParent(ModuleDesc parent) {
        this.parent = parent;
    }

    public String getFullName() {
        StringBuilder fullName = new StringBuilder();
        if (null != parent) {
            fullName.append(parent.getFullName()).append("-");
        }
        fullName.append(this.getName());
        return fullName.toString();
    }

    public FormData getData() {
        return data;
    }

    public void setData(FormData data) {
        this.data = data;
    }

    public Set<String> getModuleFacades() {
        return moduleFacades;
    }

    public void setModuleFacades(Set<String> moduleFacades) {
        this.moduleFacades = moduleFacades;
    }

    public void addModuleFacade(String moduleFacade) {
        if (null == moduleFacades) {
            moduleFacades = new HashSet<>();
        }
        moduleFacades.add(moduleFacade);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ModuleDesc other = (ModuleDesc) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

    @Override
    public int compareTo(ModuleDesc o) {
        if (null != this.getParent() && null != o.getParent()) {
            if (this.getParent().getOrdinal() == o.getParent().getOrdinal()) {
                return Integer.valueOf(this.getOrdinal()).compareTo(o.getOrdinal());
            }
            return Integer.valueOf(this.getParent().getOrdinal()).compareTo(o.getParent().getOrdinal());
        } else if (null != this.getParent()) {
            return 1;
        } else {
            return -1;
        }
    }

    @Override
    public String toString() {
        return this.getFullName();
    }

}
